<template>
  <el-cascader 
  :options="treeData" 
  size="mini" 
  :props="props" 
  style="width: 100%;" 
  separator="—" 
  :value="value"
  @change="changeValue"/>
</template>
<script>
import { getDepartSerice} from '@/api/department';
import { tranListToTreeData } from '@/utils';
export default {
  data(){
    return {
      treeData: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  props: {
    value: {
        type: Number,
        default: null
    }
  },
  created(){
       this.getDeptList()
  },
  methods: {
    async getDeptList(){
     const res = await getDepartSerice()
     const data = tranListToTreeData(res, 0)
     this.treeData = data
    },
    changeValue(list){
       if(list.length > 0){
        //  将最后一位的id取出来，然后传出去。传给父组件
          this.$emit('input', list[list.length - 1])
      }else {
          this.$emit ('input', null)
      }
    }
  }
}
</script>

<style>

</style>